package com.wn.handler;

import com.wn.mappper.BookMapper;
import com.wn.bean.Book;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyXxlJob {
    private static Logger logger = LoggerFactory.getLogger(MyXxlJob.class);

    @Autowired
    private BookMapper bookMapper;

    /**
     * 2、分片广播任务
     */
    @XxlJob("shardingJobHandler")
    public void shardingJobHandler() throws Exception {

        // 分片参数
        //拿到 分页下标
        int shardIndex = XxlJobHelper.getShardIndex();
        //分片的总数
        int shardTotal = XxlJobHelper.getShardTotal();

        System.out.println("shardIndex = " + shardIndex);
        System.out.println("shardTotal = " + shardTotal);
        logger.debug("分片参数：当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal);

        // 分片查询
        List<Book> data = bookMapper.findData(shardTotal, shardIndex);
        data.forEach(System.out::println);
    }
}